// pages/checkFlight/checkFlight.js
import { getInfoByCity, getInfoByNumber, getPlaneInfo, getPlaneList } from '../../../api/checkFlight/checkFlight.js'
var utils = require('../../../utils/util')
import { _t } from '../../../i18n/base'

Page({
  data: {
    index: true,
    ymdDate: '',
    datas: [],
    flag: false,
    language: '',
    domesticSearch: {
      endcity: '',
      city: '',
      date: '',
    },
    // 境外
    abroadSearch: {
      END_CH: '',
      START_DATE: '',
      START_EN: '',
      startCityTxt: '',
      endCityTxt: '',
    },
    date: '',
    type: '',
    showTips: false,
  },
  // 复制网址
  copyLinkHandle(e) {
    //香港国际
    let XGUrl = {
      zh: 'https://www.hongkongairport.com/sc/flights/departures/passenger.page',
      tw: 'https://www.hongkongairport.com/tc/flights/departures/passenger.page',
      en: 'https://www.hongkongairport.com/en/flights/departures/passenger.page',
    }
    //澳门国际
    let AMUrl = {
      zh: 'https://www.macau-airport.com/cn/flight-information/flight-time-table/departure',
      tw: 'https://www.macau-airport.com/mo/flight-information/flight-time-table/departure',
      en: 'https://www.macau-airport.com/en/flight-information/flight-time-table/departure',
    }
    //中航信
    let ZHUrl = { 1: '', 2: '', 3: '' }
    let type = e.currentTarget.dataset.type
    let language = wx.getStorageSync('language')
    let url = ''
    if (type == 1) {
      url = XGUrl[language]
    } else if (type == 2) {
      url = AMUrl[language]
    } else {
      url = ZHUrl[language]
    }
    if (url == undefined) {
      wx.showToast({
        title: this.data.lang['当前没有该网址'],
        icon: 'none',
      })
    }
    wx.setClipboardData({
      data: url,
      success: () => {
        wx.showToast({
          title: this.data.lang['已复制网站网址'],
          icon: 'none',
        })
      },
    })
  },
  // 根据起降地查询航班(境内)
  async getInfoByCity(e) {
    let res = await getInfoByCity(e)
    if (res.code == 200) {
      if (res.result) {
        this.setData({
          datas: res.result.list,
        })
      } else {
        this.setData({
          datas: [],
          showTips: true,
        })
      }
    }
  },
  // 根据起降地查询航班(境外)
  async getPlaneList(e) {
    let res = await getPlaneList(e)
    if (res.result.JOURNEY_DETAIL.length != 0) {
      this.setData({
        datas: res.result.JOURNEY_DETAIL,
      })
    } else {
      this.setData({
        showTips: true,
      })
    }
  },
  // 前一天
  beforeDay() {
    let d = this.data.ymdDate.y + '/' + this.data.ymdDate.m + '/' + this.data.ymdDate.d
    this.getDay(d, -1)
    if (this.data.type == 1) {
      this.getInfoByCity(this.data.domesticSearch)
    } else {
      this.getPlaneList(this.data.abroadSearch)
    }
  },
  // 后一天
  afterDay() {
    let d = this.data.ymdDate.y + '/' + this.data.ymdDate.m + '/' + this.data.ymdDate.d
    this.getDay(d, 1)
    if (this.data.type == 1) {
      this.getInfoByCity(this.data.domesticSearch)
    } else {
      this.getPlaneList(this.data.abroadSearch)
    }
  },
  // 格式日期
  getDay(date, num) {
    let n = 0
    if (num == 1) {
      n = 1
    } else if (num == -1) {
      n = -1
    }
    let d = new Date(date)
    d.setDate(d.getDate() + n)
    let ymdDate = utils.formatYMD(d)
    let year = ymdDate.y
    let month = ymdDate.m.toString().padStart(2, '0')
    let day = ymdDate.d.toString().padStart(2, '0')
    this.setData({
      ymdDate,
      ['domesticSearch.date']: `${ymdDate.y}-${ymdDate.m}-${ymdDate.d}`,
      ['abroadSearch.date']: `${ymdDate.y}-${ymdDate.m}-${ymdDate.d}`,
      date: `${year}-${month}-${day}`,
    })
  },
  // 复制
  flghtNumCopHandle({ currentTarget }) {
    wx.setClipboardData({
      data: currentTarget.dataset.fight,
      success: () => {
        wx.showToast({
          title: this.data.lang['已复制航班号'],
        })
      },
    })
  },
  // 初始化
  async onLoad(options) {
    this.setData({
      lang: _t(),
      language: wx.getStorageSync('lang'),
    })
    wx.setNavigationBarTitle({
      title: this.data.lang['航班查询'],
    })
    if (options.flightInfo) {
      let info = JSON.parse(options.flightInfo)
      this.setData({
        type: info.borderType,
      })
      // 境内
      if (info.borderType == 1) {
        // 航号
        if (info.flightno) {
          let res = await getInfoByNumber(info)
          if (res.result.arrival) {
            res.result.startTime = res.result.depart.split(' ')[1].substring(0, 5)
            res.result.endTime = res.result.arrival.split(' ')[1].substring(0, 5)
            let list = []
            list.push(res.result)
            this.setData({
              datas: list,
              flag: false,
            })
          } else {
            this.setData({
              showTips: true,
            })
          }
        }
        // 起降地
        if (info.dCityTxt) {
          this.setData({
            flag: true,
            ['domesticSearch.endcity']: info.aCityTxt,
            ['domesticSearch.city']: info.dCityTxt,
            ['domesticSearch.date']: info.date,
          })
          this.getDay(info.date)
          this.getInfoByCity(this.data.domesticSearch)
        }
      }
      // 境外
      else {
        // 航班号
        if (info.flightno) {
          let obj = {
            DATE: info.date.replace(/[-]/g, ''),
            FLIGHT_ID: info.flightno,
          }
          let { result } = await getPlaneInfo(obj)
          if (result[0].FLIGHT_ID) {
            this.setData({
              datas: result,
              num: 1,
            })
          } else {
            this.setData({
              datas: [],
              showTips: true,
            })
          }
        }
        // 起降地
        if (info.dCityTxt) {
          this.setData({
            flag: true,
            num: 2,
            ['abroadSearch.END_CH']: info.abridgeACityTxt,
            ['abroadSearch.START_DATE']: info.date,
            ['abroadSearch.START_EN']: info.abridgeDCityTxt,
            ['abroadSearch.startCityTxt']: info.dCityTxt,
            ['abroadSearch.endCityTxt']: info.aCityTxt,
          })
          // 再次格式日期
          this.setData({
            ['abroadSearch.START_DATE']: this.data.abroadSearch.START_DATE.replace(/[-]/g, ''),
          })
          this.getDay(info.date)
          this.getPlaneList(this.data.abroadSearch)
        }
      }
    }
  },
})
